package com.jlnk.rxjavademo.view;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.jlnk.rxjavademo.R;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.os.Bundle;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.jlnk.rxjavademo.presenter.ImageDetailPresenter;

public class ImageDetailActivity extends AppCompatActivity implements ImageDetailPresenter.ImageDetailView {
    private ImageDetailPresenter imageDetailPresenter;
    private ImageView imageView;
    private ProgressBar progressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_detail);

        imageView = findViewById(R.id.imageView);
        progressBar = findViewById(R.id.progressBar);
        String imageUrl = getIntent().getStringExtra("imageUrl");
        imageDetailPresenter = new ImageDetailPresenter(this, imageUrl);
        imageDetailPresenter.loadImage();

        imageView.setOnLongClickListener(v -> {
            showSaveImageDialog();
            return true;
        });
    }

    private void showSaveImageDialog() {
        new AlertDialog.Builder(this)
                .setTitle("保存图片")
                .setMessage("是否要将这张图片保存到您的相册？")
                .setPositiveButton("保存", (dialog, which) -> imageDetailPresenter.saveImageToGallery())
                .setNegativeButton("取消", null)
                .show();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 1 && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            imageDetailPresenter.saveImageToGallery();
        }
    }

    @Override
    public void showLoading() {
        progressBar.setVisibility(ProgressBar.VISIBLE);
    }

    @Override
    public void hideLoading() {
        progressBar.setVisibility(ProgressBar.GONE);
    }

    @Override
    public ImageView getImageView() {
        return imageView;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    public void showSaveSuccess() {
        Toast.makeText(this, "图片保存成功", Toast.LENGTH_SHORT).show();
    }
}